// pages/media/index.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    navSelectIndex: 0,
    page: 1,
    listData: [],
    // 显示加载更多 loading
    hothidden: true,
    keyword: "",
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    Object.assign(this, ImgListCell);

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    this.loadListData();
  },
  searchContentInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  searchSubmit: function () {
    var that = this;
    that.loadListData();
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    this.loadListData();
  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;

    var formData = {};

    var url = apiUrl.UrlYumList;

    if (that.data.navSelectIndex > 0) {
      //所有的
      formData['type'] = 1;
      formData['keyword'] = that.data.keyword;
    } else {
      formData['type'] = 2;
    }

    formData['page'] = page;

    http.ApiGet(url, formData, function (res) {
      var listData = res.list;
      // 获取当前现有数据进行保存
      var list = that.data.listData;
      if (listData && listData.length > 0) {

        listData = listData.map(function (item) {
          return item;
        })

        that.setData({
          listData: list.concat(listData),
          page: page,
          hothidden: false
        });
      } else {
        that.setData({
          hothidden: false
        });
      }
      wx.hideNavigationBarLoading() //完成停止加载
    });

  },
  loadListData: function () {
    var that = this;
    var formData = {};


    var url = apiUrl.UrlYumList;

    if (that.data.navSelectIndex > 0) {
      //所有的
      formData['type'] = 1;
      formData['keyword'] = that.data.keyword;
    } else {
      formData['type'] = 2;
    }

    formData['page'] = 1;

    http.ApiGet(url, formData, function (res) {
      var listData = res.list;
      // console.log(res.data);
      if (listData && listData.length > 0) {
        listData = listData.map(function (item) {
          return item;
        })
        that.setData({
          listData: listData,
          hothidden: false
        });
      } else {
        that.setData({
          listData: [],
          hothidden: false
        });
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },
  tapAtIndex: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var theItem = that.data.listData[index];

    // console.log(theItem.uid);
    if (that.data.navSelectIndex == 1) {
      //所有的可绑定
      wx.showModal({
        title: theItem.appname,
        content: theItem.intro,
        confirmText: '绑定',
        success: function (res) {
         
          if (res.confirm) {
            // console.log('用户点击绑定');

            var postData = {};
            postData["id"] = theItem.id;
            postData["appname"] = theItem.appname;
            that.doBind(postData);
          }
        }
      });
    } else {
      wx.showModal({
        title: theItem.appname,
        content: theItem.intro,
      });
    }
  },
  doBind: function (postData) {
    http.ApiPost(apiUrl.UrlYumBind, postData, function (res) {
      if (res.result.errorCode == 1) {
        wx.showToast({
          title: '绑定成功！',
          icon: 'success',
          duration: 1000
        });
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  navItemTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.id;
    that.setData({
      navSelectIndex: index,
      page: 1
    });

    that.loadListData();
  }
})